package com.steed.rocketmq.producer.config.ons;

import com.aliyun.openservices.ons.api.ONSFactory;
import com.aliyun.openservices.ons.api.Producer;
import com.aliyun.openservices.ons.api.PropertyKeyConst;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Properties;

/**
 * ONS 配置类
 *
 * @author za-zhubin002
 * Created in 14:23 2017/10/27
 */
@Configuration
@EnableConfigurationProperties(OnsProducerProperties.class)
public class OnsProducerConfiguration {

    @Bean
    public Producer producer(OnsProducerProperties onsProducerProperties){
        Properties properties = new Properties();
        // MQ控制台创建的Producer ID
        properties.put(PropertyKeyConst.ProducerId, onsProducerProperties.getProducer_id());
        // 鉴权用AccessKey，在阿里云服务器管理控制台创建
        properties.put(PropertyKeyConst.AccessKey, onsProducerProperties.getAccess_key());
        // 鉴权用SecretKey，在阿里云服务器管理控制台创建
        properties.put(PropertyKeyConst.SecretKey, onsProducerProperties.getSecret_key());
        // 设置 TCP 接入域名
        properties.put(PropertyKeyConst.ONSAddr, onsProducerProperties.getOns_addr());
        Producer producer = ONSFactory.createProducer(properties);
        producer.start();
        return producer;
    }

}
